from datetime import datetime

from IDCardOCR.IDCard import IDCard, IdCardInfo, extract_id_card_number,check_idcard,extract_valid_date
from pywebsite.controller_paddleocr import get_ocr_result
from pywebsite.controller_paddleocr import get_ocr_result_img
from PIL import Image

from decimal import Decimal
import decimal
import re


def check_idcard_img(filepath, holder=0.5, debug=False):
    result1 = get_ocr_result(filepath)
    # 生产环境有[0]: result = result1[0]
    result = result1[0]

    #下一行为姓名
    nameflag=False
    namebak=""

    step=-1
    name=""
    id_number=""
    gender=""
    birthdate=""
    address=""
    addrflag=False
    ethnic=""
    type = 0
    start_date=""
    end_date=""

    for index,line in enumerate(result):
        if index == 0 and "中" in line[1][0]:
            step-=1
            type=1
        elif index == 0:
            step+=1

        #name
        if step==0:
            if index!=0 and nameflag:
                name=line[1][0]
                nameflag=False
                step+=1
            if index==0 and '姓名' not in line[1][0]:
                namebak=line[1][0]

            if '姓名' in line[1][0]:
                if index==0:
                    if "姓名"==line[1][0]:
                        nameflag = True
                    else:
                        name=line[1][0].replace('姓名', '')
                        step+=1
                if index==1:
                    name=namebak
                    step+=1
        # gender
        if step == 1:
            if '别' in line[1][0]:
                step+=1

        # birthdate bak
        if step == 2:
            if '族' in line[1][0]:
                ethnic = line[1][0].split('族')[1]
                step+=1

        # birthdate bak
        if step == 3:
            if '年' in line[1][0] and "月" in line[1][0] and "日" in line[1][0]:
                # birthdate = extract_date_from_string(line[1][0])
                step += 1
        # address
        if step == 4:
            if '址' in line[1][0]:
                address = address+line[1][0].split('址')[1]
                addrflag = True
                continue
            if "公民身份号码" in line[1][0]:
                addrflag = False
                step+=1
            if addrflag:
                address = address+line[1][0]

        #id_number
        if step == 5:
            id_card_number = extract_id_card_number(line[1][0])
            if id_card_number:
                id_number = id_card_number
                step+=1

        #else
        if step == 6:
            card_info = IdCardInfo(id_number)
            gender = card_info.gender
            birthdate = card_info.birthday
            step+=1

        #valid_date
        if step == -2:
            dates = extract_valid_date(line[1][0])
            if dates is not None:
                start_date, end_date = dates
                step-=1


    image = Image.open(filepath).convert('RGB')

    boxes = [line[0] for line in result]
    txts = [line[1][0] for line in result]
    scores = [line[1][1] for line in result]
    ret_img = get_ocr_result_img(image, boxes, txts, scores)

    if type==0:
        return IDCard(card_type=0,name=name,id_number=id_number,gender=gender,birthdate=birthdate,ethnic=ethnic,address=address,valid=step==7 and check_idcard(id_number)), ret_img
    else:
        return IDCard(card_type=1,valid_date_start=start_date,valid_date_end=end_date,valid=step==-3 and type==1), ret_img



